#!/usr/bin/env python

import os

from os.path import exists, join
from seisflows.tools import unix


def listdir(dir):
    subdirs = []
    for subdir in unix.ls(dir):
        if os.path.isdir(dir+'/'+subdir):
            if subdir[0] != '_':
                subdirs += [subdir]
    subdirs.sort()
    return subdirs


def getpath():
    path = os.getenv('SEISFLOWS_EXAMPLES')
    if not exists(path):  
       raise EnvironmentError()
    return path


if __name__ == '__main__':
    """ Prompts user to select SEISFLOWS example, then sets up working directory
      to run example.
    """
    if unix.hostname() in ['tiger1', 'tiger2', 'tiger', 'tigergpu']:
        path = '/home/rmodrak/data'
    else:
        path = getpath()
    print ''

    # select directory
    dirs = ['Examples2d', 'Examples3d', 'Examples3dGlobe']
    dir = unix.select(dirs)
    print ''

    # select subdirectory
    subdirs = listdir(path+'/'+dir)
    subdir = unix.select(subdirs)
    print ''

    # set up working directory
    fullpath = path+'/'+dir+'/'+subdir
    unix.cp(fullpath+'/'+'parameters.py', '.')
    unix.cp(fullpath+'/'+'paths.py', '.')

